package com.mhdanh.serviceimpl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mhdanh.service.ExcelExportService;
import com.mhdanh.service.ExcelService;

@Service
public class ExcelExportServiceImpl implements ExcelExportService{
	
	@Autowired
	ExcelService excelService;
	
	
	public void writeFileToResponse(
			ByteArrayOutputStream baos, String fileName,String type,HttpServletResponse servletResponse) {
		try {
			servletResponse.setContentType(type);
			servletResponse.setHeader("Expires", "0");
			servletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			servletResponse.setHeader("Pragma", "public");
			servletResponse.setHeader("Content-disposition", "attachment;filename=" + fileName);
			servletResponse.setContentLength(baos.size());
			OutputStream out = servletResponse.getOutputStream();
			baos.writeTo(out);
			servletResponse.flushBuffer();
			
			// for jsf
//			FacesContext context = FacesContext.getCurrentInstance();
//			ExternalContext externalContext = context.getExternalContext();
//		
//			externalContext.setResponseContentType(type);
//			externalContext.setResponseHeader("Expires", "0");
//			externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
//			externalContext.setResponseHeader("Pragma", "public");
//			externalContext.setResponseHeader("Content-disposition", "attachment;filename=" + fileName);
//			externalContext.setResponseContentLength(baos.size());
//			OutputStream out = externalContext.getResponseOutputStream();
//			baos.writeTo(out);
//			externalContext.responseFlushBuffer();
//			
//			context.responseComplete();
			//end for jsf
		} catch (Exception e) {}
		
	}

	@Override
	public void exportExcel(HttpServletResponse servletResponse) throws WriteException, IOException {
		ArrayList<ArrayList<String>> arrayLists = new ArrayList<ArrayList<String>>();
		ArrayList<String> titleIssue= new ArrayList<String>();
		
		titleIssue.add("Title cot 1");
		titleIssue.add("Title cot 2");
		titleIssue.add("Title cot 3");
		
		arrayLists.add(titleIssue);
		
		for(int i = 0; i< 10;i++)
		{
			ArrayList<String> strings= new ArrayList<String>();
			strings.add("data-cot1");
			strings.add("data-cot2");
			strings.add("data-col3");
			
			arrayLists.add(strings);
		}
		String name="exportexcel_" +  new Date().getTime();
		excelService.setContent(name,arrayLists);
	    this.writeFileToResponse(excelService.writeASheet(), name+".xls", "application/vnd.ms-excel; charset=UTF-8",servletResponse);
	}
}
